module.exports=app=>{
  const router = require('express').Router()
  //const mongoose = require('mongoose')
  //const Article = mongoose.model('Article')
  //const Category = mongoose.model('Category')
  const Category = require('../../models/Category')
  const Article = require('../../models/Article')
  const Hero = require('../../models/Hero')
  const Item = require('../../models/Item')
  const Ad = require('../../models/Ad')
  const Video = require('../../models/Video')
  const Newhero = require('../../models/Newhero')
  //init news
  router.get('/news/init',async(req,res)=>{
    const parent = await Category.findOne({
      title: '新闻分类'
    })
    const cats = await Category.find().where({
      parent
    }).lean()//只获取新闻资讯下属的分类
    const newsTitles = ["正式服预告④丨新版本倒数3天，战场升级计划即将启动", "正式服预告③丨新英雄镜即将登场，将军白起优化升级！", "版本动画《玄雍梦魇》·镜先导片上线！", "正式服预告②丨荣耀战令福利更新，峡谷历险即刻启程", "正式服预告①丨玄雍危机降临，S19新赛季来袭！", "4月“国服最强”荣耀称号获取规则说明", "【已开服】3月27日抢先服“玄雍危机”版本更新公告", "梦境修炼活动提前结束公告说明", "3月24日全服不停机更新公告", "3月28日体验服停机更新公告", "抢先服更新 S19赛季开启", "微信游戏中心微券功能说明(二期)", "【奇遇 咕嗒的寻宝之旅】活动公告", "梦境修炼活动提前结束公告说明", "【赛末冲刺领豪礼】活动开启公告", "【KPL今日预报】武汉ES vs 重庆QG，武汉eStar春季赛首秀能否首胜？", "高校联赛解说大赛正式开启报名", "【KPL今日预报】WE vs 成都AG超玩会，成都AG能否收获三连胜？", "3月28日部分比赛时间调整公告", "触手王者荣耀新秀杯报名启动 全国大赛邀请赛名额等你来战"]
    //const catsRandom = cats.slice(0).sort((a,b)=>Math.random()-0.5)
    const newsList = newsTitles.map((title)=>{
      const catsRandom = cats.slice(0).sort((a, b) => Math.random() - 0.5)
      return{
        categories: catsRandom.slice(0,2),
        title:title
      }
    })
    await Article.deleteMany({})//删除数据
    await Article.insertMany(newsList)
    res.send({
      newsList
    })
  })
  //获取news
  router.get('/news/list',async(req,res)=>{
    /* const parent = await Category.findOne({
      title: '新闻资讯'
    }).populate({
      path:'children'
    }).lean()//获取新闻资讯下面的子分类 */

    /* const parent = await Category.findOne({
      title: '新闻资讯'
    }).populate({
      path: 'children',
      populate:{
        path:'newsList'
      }
    }).lean() //获取新闻资讯下面的子分类的子分类  新闻资讯>新闻>文章 */

    //聚合查询
    const parent = await Category.findOne({
      title: '新闻分类'
    })
    const cats = await Category.aggregate([
      { $match:{ parent:parent._id}},
      { 
        $lookup:{
          from:'articles',
          localField:'_id',
          foreignField:'categories',
          as:'newsList'
        }
      },
      {
        $addFields:{
          newsList:{$slice:['$newsList',5]}//取5个
        }
      }
    ])
    const  subCats = cats.map(v=>v._id)
    cats.unshift({//添加热门
      title:'热门',
      newsList:await Article.find().where({
        categories:{$in:subCats}
      }).populate('categories').limit(5).lean()
    })
    cats.map(cat=>{//子子类添加标签
      cat.newsList.map(news=>{ 
        news.categoryName=(cat.title==='热门')?news.categories[0].title:cat.title
        return news
      })
      return cat
    })
    res.send(cats)
  })
//init heroes
router.get('/heroes/init',async(req,res)=>{
  await Hero.deleteMany({}) //删除数据
  const rawData = [{
    "name": "热门",
    "heroes": [{
      "name": "后羿",
      "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/169/169.jpg"
    }, {
      "name": "孙悟空",
      "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/167/167.jpg"
    }, {
      "name": "铠",
      "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg"
    }, {
      "name": "安琪拉",
      "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/142/142.jpg"
    }, {
      "name": "亚瑟",
      "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/166/166.jpg"
    }, {
      "name": "鲁班七号",
      "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/112/112.jpg"
    }, {
      "name": "妲己",
      "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/109/109.jpg"
    }, {
      "name": "甄姬",
      "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/127/127.jpg"
    }, {
      "name": "韩信",
      "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/150/150.jpg"
    }, {
      "name": "伽罗",
      "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/508/508.jpg"
        }]
      }, {
        "name": "战士",
        "heroes": [{
          "name": "赵云",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/107/107.jpg"
        }, {
          "name": "墨子",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/108/108.jpg"
        }, {
          "name": "钟无艳",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/117/117.jpg"
        }, {
          "name": "吕布",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/123/123.jpg"
        }, {
          "name": "夏侯惇",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/126/126.jpg"
        }, {
          "name": "曹操",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/128/128.jpg"
        }, {
          "name": "典韦",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/129/129.jpg"
        }, {
          "name": "宫本武藏",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/130/130.jpg"
        }, {
          "name": "达摩",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/134/134.jpg"
        }, {
          "name": "老夫子",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/139/139.jpg"
        }, {
          "name": "关羽",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/140/140.jpg"
        }, {
          "name": "程咬金",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/144/144.jpg"
        }, {
          "name": "露娜",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/146/146.jpg"
        }, {
          "name": "花木兰",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/154/154.jpg"
        }, {
          "name": "橘右京",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/163/163.jpg"
        }, {
          "name": "亚瑟",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/166/166.jpg"
        }, {
          "name": "孙悟空",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/167/167.jpg"
        }, {
          "name": "刘备",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/170/170.jpg"
        }, {
          "name": "钟馗",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/175/175.jpg"
        }, {
          "name": "杨戬",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/178/178.jpg"
        }, {
          "name": "雅典娜",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/183/183.jpg"
        }, {
          "name": "哪吒",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/180/180.jpg"
        }, {
          "name": "铠",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg"
        }, {
          "name": "苏烈",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg"
        }, {
          "name": "裴擒虎",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/502/502.jpg"
        }, {
          "name": "狂铁",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/503/503.jpg"
        }, {
          "name": "孙策",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/510/510.jpg"
        }, {
          "name": "李信",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/507/507.jpg"
        }, {
          "name": "盘古",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/529/529.jpg"
        }, {
          "name": "云中君",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/506/506.jpg"
        }, {
          "name": "曜",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/522/522.jpg"
        }, {
          "name": "马超",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/518/518.jpg"
        }]
      }, {
        "name": "法师",
        "heroes": [{
          "name": "小乔",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/106/106.jpg"
        }, {
          "name": "墨子",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/108/108.jpg"
        }, {
          "name": "妲己",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/109/109.jpg"
        }, {
          "name": "嬴政",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/110/110.jpg"
        }, {
          "name": "高渐离",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/115/115.jpg"
        }, {
          "name": "孙膑",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/118/118.jpg"
        }, {
          "name": "扁鹊",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/119/119.jpg"
        }, {
          "name": "芈月",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/121/121.jpg"
        }, {
          "name": "周瑜",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/124/124.jpg"
        }, {
          "name": "甄姬",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/127/127.jpg"
        }, {
          "name": "武则天",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/136/136.jpg"
        }, {
          "name": "貂蝉",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/141/141.jpg"
        }, {
          "name": "安琪拉",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/142/142.jpg"
        }, {
          "name": "露娜",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/146/146.jpg"
        }, {
          "name": "姜子牙",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/148/148.jpg"
        }, {
          "name": "王昭君",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/152/152.jpg"
        }, {
          "name": "张良",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/156/156.jpg"
        }, {
          "name": "不知火舞",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/157/157.jpg"
        }, {
          "name": "钟馗",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/175/175.jpg"
        }, {
          "name": "诸葛亮",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/190/190.jpg"
        }, {
          "name": "干将莫邪",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/182/182.jpg"
        }, {
          "name": "女娲",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/179/179.jpg"
        }, {
          "name": "杨玉环",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/176/176.jpg"
        }, {
          "name": "弈星",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/197/197.jpg"
        }, {
          "name": "米莱狄",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/504/504.jpg"
        }, {
          "name": "司马懿",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/137/137.jpg"
        }, {
          "name": "沈梦溪",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/312/312.jpg"
        }, {
          "name": "上官婉儿",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/513/513.jpg"
        }, {
          "name": "嫦娥",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/515/515.jpg"
        }, {
          "name": "西施",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/523/523.jpg"
        }]
      }, {
        "name": "坦克",
        "heroes": [{
          "name": "廉颇",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/105/105.jpg"
        }, {
          "name": "庄周",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/113/113.jpg"
        }, {
          "name": "刘禅",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/114/114.jpg"
        }, {
          "name": "钟无艳",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/117/117.jpg"
        }, {
          "name": "白起",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/120/120.jpg"
        }, {
          "name": "芈月",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/121/121.jpg"
        }, {
          "name": "吕布",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/123/123.jpg"
        }, {
          "name": "夏侯惇",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/126/126.jpg"
        }, {
          "name": "达摩",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/134/134.jpg"
        }, {
          "name": "项羽",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/135/135.jpg"
        }, {
          "name": "程咬金",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/144/144.jpg"
        }, {
          "name": "刘邦",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/149/149.jpg"
        }, {
          "name": "亚瑟",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/166/166.jpg"
        }, {
          "name": "牛魔",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/168/168.jpg"
        }, {
          "name": "张飞",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/171/171.jpg"
        }, {
          "name": "太乙真人",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/186/186.jpg"
        }, {
          "name": "东皇太一",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/187/187.jpg"
        }, {
          "name": "铠",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg"
        }, {
          "name": "苏烈",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg"
        }, {
          "name": "梦奇",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/198/198.jpg"
        }, {
          "name": "孙策",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/510/510.jpg"
        }, {
          "name": "嫦娥",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/515/515.jpg"
        }, {
          "name": "猪八戒",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/511/511.jpg"
        }]
      }, {
        "name": "刺客",
        "heroes": [{
          "name": "赵云",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/107/107.jpg"
        }, {
          "name": "阿轲",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/116/116.jpg"
        }, {
          "name": "李白",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/131/131.jpg"
        }, {
          "name": "貂蝉",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/141/141.jpg"
        }, {
          "name": "韩信",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/150/150.jpg"
        }, {
          "name": "兰陵王",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/153/153.jpg"
        }, {
          "name": "花木兰",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/154/154.jpg"
        }, {
          "name": "不知火舞",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/157/157.jpg"
        }, {
          "name": "娜可露露",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/162/162.jpg"
        }, {
          "name": "橘右京",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/163/163.jpg"
        }, {
          "name": "孙悟空",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/167/167.jpg"
        }, {
          "name": "百里守约",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg"
        }, {
          "name": "百里玄策",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg"
        }, {
          "name": "裴擒虎",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/502/502.jpg"
        }, {
          "name": "元歌",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/125/125.jpg"
        }, {
          "name": "司马懿",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/137/137.jpg"
        }, {
          "name": "上官婉儿",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/513/513.jpg"
        }, {
          "name": "云中君",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/506/506.jpg"
        }, {
          "name": "马超",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/518/518.jpg"
        }]
      }, {
        "name": "射手",
        "heroes": [{
          "name": "孙尚香",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/111/111.jpg"
        }, {
          "name": "鲁班七号",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/112/112.jpg"
        }, {
          "name": "马可波罗",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/132/132.jpg"
        }, {
          "name": "狄仁杰",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/133/133.jpg"
        }, {
          "name": "后羿",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/169/169.jpg"
        }, {
          "name": "李元芳",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/173/173.jpg"
        }, {
          "name": "虞姬",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/174/174.jpg"
        }, {
          "name": "成吉思汗",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/177/177.jpg"
        }, {
          "name": "黄忠",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/192/192.jpg"
        }, {
          "name": "百里守约",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg"
        }, {
          "name": "公孙离",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/199/199.jpg"
        }, {
          "name": "伽罗",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/508/508.jpg"
        }, {
          "name": "蒙犽",
          "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/524/524.jpg"
        }]
      }, {
      "name": "辅助",
      "heroes": [{
        "name": "庄周",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/113/113.jpg"
      }, {
        "name": "刘禅",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/114/114.jpg"
      }, {
        "name": "孙膑",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/118/118.jpg"
      }, {
        "name": "姜子牙",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/148/148.jpg"
      }, {
        "name": "牛魔",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/168/168.jpg"
      }, {
        "name": "张飞",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/171/171.jpg"
      }, {
        "name": "蔡文姬",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/184/184.jpg"
      }, {
        "name": "太乙真人",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/186/186.jpg"
      }, {
        "name": "大乔",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/191/191.jpg"
      }, {
        "name": "鬼谷子",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/189/189.jpg"
      }, {
        "name": "明世隐",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/501/501.jpg"
      }, {
        "name": "杨玉环",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/176/176.jpg"
      }, {
        "name": "盾山",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/509/509.jpg"
      }, {
        "name": "瑶",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/505/505.jpg"
      }, {
        "name": "鲁班大师",
        "avatar": "https://game.gtimg.cn/images/yxzj/img201606/heroimg/525/525.jpg"
      }]
  }]
  for(let cat of rawData){
    /* if(cat.name==='热门'){
      continue
    } */
    //找到当前分类在数据库中对应的数据
    const category = await Category.findOne({
      title:cat.name
    })
    //console.log(category);
    cat.heroes.map(hero => {
      if (category) {
        hero.categories = [category]
      }
      //return {categories:category,hero}
      //console.log(hero);
      
      return hero
    })
    //录入英雄
    await Hero.insertMany(cat.heroes)
  }
  res.send(await Hero.find())
})
//init item
router.get('/items/init',async(req,res)=>{
  await Item.deleteMany({})
  const itemData=[{"name":"抵抗之靴","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/c38de4a1127ff2aa50bf8bb734c1bd5d.png"},{"name":"符文大剑","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/0ab25a41a179f35c458fa9db521d3e9a.png"},{"name":"极影","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/2c1de684368efaf4a2d9e32442989207.png"},{"name":"急速战靴","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/ff49c0c86be154ff699748252d443cb4.png"},{"name":"近卫荣耀","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/ed334035206363d2e177618aefbb78cc.png"},{"name":"救赎之翼","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/75957e219ff241608c29d5c3e4684ed1.png"},{"name":"巨人之握","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/d168b3639676ee3ddefd7162e5ba6ed8.png"},{"name":"冷静之靴","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/585ace450455e89922aa40fcf17af584.png"},{"name":"秘法之靴","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/fbadbc8c3014867b004430f9c13b037d.png"},{"name":"贪婪之噬","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/af96ebbe49db07ccb824533e61924ffa.png"},{"name":"学识宝石","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191105/c16416fb73156c7515d12b425081d4cd.png"},{"name":"影忍之足","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/76b2e204a25fb9925f6cdadebe0632c7.png"},{"name":"不祥征兆","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/550eb2c03132b9a196044484fc5444d1.png"},{"name":"冰霜长矛","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191122/aa6419f68fadc0ce8ba76226b3faaf10.png"},{"name":"爆裂之甲","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191125/82c0bd6a784c991c20825efcd4411d49.png"},{"name":"刺客令旗","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191210/9d58d7458f915f86891e27c56bc572cf.png"},{"name":"法师令旗","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191210/c01ec611c493ef63a678f909db65d651.png"},{"name":"反伤刺甲","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191216/b724a404184ac887ec8dcd437c50b118.png"},{"name":"红莲斗篷","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/d3bf76ace22d62280dfc587b2888d2e9.png"},{"name":"辉月","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/57c2771684ba3f46460366b6eabb59bc.png"},{"name":"回响之杖","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/8daee00cd91bf731db9381e954c2e3a7.png"},{"name":"极寒风暴","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/5df2d1bbec654194f756fdf33f545ce1.png"},{"name":"梦魇之牙","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/fd56b2852d1c4af4a2fc7b6c784627ce.png"},{"name":"名刀·司命","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/03d316a6577287f58714f1d27c76e318.png"},{"name":"魔女斗篷","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/8b79605da16329f57d8447e60274fad4.png"},{"name":"末世","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/8391bdb338b1d8058bac03b6bde2c6c6.png"},{"name":"泣血之刃","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/0836ad1cfeee7faefa8f42e15172b0e9.png"},{"name":"闪电匕首","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/0bc6d644e096f7e1a29d1491618b86b1.png"},{"name":"噬神之书","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/c1fd1f27dfc4a72907fada5e4ab8091c.png"},{"name":"碎星锤","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/83b74d40c0c96d96ffd19c7ff9a68972.png"},{"name":"射手令旗","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191210/4653c8b8b7af364526323e3b85b571a3.png"},{"name":"坦克令旗","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191210/656d388b4058753714538327ee147174.png"},{"name":"无尽战刃","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/a21592530ddb7c2dc659f34682b63216.png"},{"name":"虚无法杖","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/d6225f9d6d4a430a6e95b4e58d2fe567.png"},{"name":"血魔之怒","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/606fbb1afcedf589ecae9877afcd0f59.png"},{"name":"影刃","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/a0fb006cae34370cc342a1f480c61a31.png"},{"name":"制裁之刃","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/d4f03bc62110d5481138aa900b8b3644.png"},{"name":"炙热支配者","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/566230aa1074c8927b1a57038e9c6226.png"},{"name":"战士令旗","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191210/cbd867f6578d38d74d7b0309616e31e0.png"},{"name":"霸者重装","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/9c1c328e0f695df53a00e495d591c54b.png"},{"name":"博学者之怒","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/408fa373177416103dc4e467fe9aecab.png"},{"name":"辅助令旗","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191210/cbe7cb497929551c484c5ee0d6e4e28e.png"},{"name":"破军","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/a7c78b463a98531489185fc5cb585ac9.png"},{"name":"破晓","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/878ca6674b7444f941c3e055d1170504.png"},{"name":"贤者的庇护","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/478734efb5322c231b07ac26edaa8d67.png"},{"name":"贤者之书","icon":"https://game.gtimg.cn/images/yxzj/zlkdatasys_04/images/image/20191008/cf12153e52fcbeac11079eefd743f0ed.png"}]
  await Item.insertMany(itemData)
  res.send(await Item.find())
})
  //获取heroes
  router.get('/heroes/list', async (req, res) => {
    const parent = await Category.findOne({
      title: '英雄分类'
    })
    const cats = await Category.aggregate([{
        $match: {
          parent: parent._id
        }
      },
      {
        $lookup: {
          from: 'heroes',
          localField: '_id',
          foreignField: 'categories._id',
          as: 'heroList'
        }
      }, 
    ])
    /* const subCats = cats.map(v => v._id)
    cats.unshift({ //添加热门
      name: '热门',
      heroList: await Hero.find().where({
        categories: {
          $in: subCats
        }
      }).limit(10).lean()
    }) */
    cats.unshift({
      title:'热门',
      heroList: await Hero.find().limit(10).lean()
    })
    res.send(cats)
  })
//文章详情接口
router.get('/articles/:id',async(req,res)=>{
  const data=await Article.findById(req.params.id).lean()
  /* data.related=await Article.find().where({
    categories:{$in:data.categories}
  }).limit(2) */
  data.related = await Article.find().limit(2)//.slice(0).sort((a, b) => Math.random() - 0.5).limit(2)
  res.send(data)
})
//hero api
router.get('/heroes/:id',async(req,res)=>{
  const heroList=await Hero.findById(req.params.id).populate('items1 items2 partners.hero').lean()//转成json对象
  const catH = await Category.findById(heroList.categories[0])
  heroList.categories=catH
  //console.log(catH);
  res.send(heroList)
})
//ads api
router.get('/ads',async(req,res)=>{
  const ads=await Ad.find().lean()
  res.send(ads)
})
//video api
router.get('/videos', async (req, res) => {
  const videos = await Video.find().lean()
  res.send(videos)
})
//newhero api
router.get('/newhero', async (req, res) => {
  const videos = await Newhero.find().lean()
  res.send(videos)
})
  app.use('/web/api',router)
}